namespace SmartSoft.ADP
{
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class DmTsDataAccess
    {
        private WSDmTsDataAccess.WSDmTsDataAccess wDataAcess = new WSDmTsDataAccess.WSDmTsDataAccess();
        private SmartSoft.DAL.DmTsDataAccess oDataAcess = new SmartSoft.DAL.DmTsDataAccess();

        public DmTsDataAccess()
        {
            wDataAcess.Url = wDataAcess.Url.Replace("localhost", AdapterInstance.ServerIp);
        }

        //public bool DeleteDetailRow(SqlConnection po_Connection, SqlTransaction po_Transaction, object po_KeyCode)
        //{
        //    if (AdapterInstance.IsRemoteConnected)
        //    {
        //        return wDataAcess.DeleteDetailRow(po_Connection, po_Transaction, po_KeyCode == DBNull.Value ? string.Empty : po_KeyCode);
        //    }
        //    else
        //    {
        //        return oDataAcess.DeleteDetailRow(po_Connection, po_Transaction, po_KeyCode == DBNull.Value ? string.Empty : po_KeyCode);
        //    }
        //}

        public bool DeleteDetailRow0(object po_MasterKey, object po_KeyCode)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return wDataAcess.DeleteDetailRow0(po_MasterKey, po_KeyCode == DBNull.Value ? string.Empty : po_KeyCode);
            }
            else
            {
                return oDataAcess.DeleteDetailRow0(po_MasterKey, po_KeyCode);
            }
        }

        public bool DeleteDetailRow2(object po_MasterKey, object po_KeyCode)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return wDataAcess.DeleteDetailRow2(po_MasterKey, po_KeyCode == DBNull.Value ? string.Empty : po_KeyCode);
            }
            else
            {
                return oDataAcess.DeleteDetailRow2(po_MasterKey, po_KeyCode);
            }
        }

        //public bool DeleteDetailRow3(SqlConnection po_Connection, SqlTransaction po_Transaction, object po_KeyCode)
        //{
        //    if (AdapterInstance.IsRemoteConnected)
        //    {
        //        return wDataAcess.DeleteDetailRow3(po_Connection, po_Transaction, po_KeyCode == DBNull.Value ? string.Empty : po_KeyCode);
        //    }
        //    else
        //    {
        //        return oDataAcess.DeleteDetailRow3(po_Connection, po_Transaction, po_KeyCode);
        //    }
        //}

        public DataTable Depreciation(string ps_Subsidiary, object pd_Ngay_Ct1, object pd_Ngay_Ct2)
        {
            DataTable table = null;
            if (AdapterInstance.IsRemoteConnected)
            {
                table = wDataAcess.Depreciation(ps_Subsidiary, pd_Ngay_Ct1, pd_Ngay_Ct2);
            }
            else
            {
                table = oDataAcess.Depreciation(ps_Subsidiary, pd_Ngay_Ct1, pd_Ngay_Ct2);
            }
            if (table != null) table.TableName = string.Empty;
            return table;
        }

        public DataSet GetDataSet(string ps_Subsidiary)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return wDataAcess.GetDataSet(ps_Subsidiary);
            }
            else
            {
                return oDataAcess.GetDataSet(ps_Subsidiary);
            }
        }

        public DataSet GetDataSet(string ps_Subsidiary, string ps_Loai_Ts)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return wDataAcess.GetDataSet(ps_Subsidiary, ps_Loai_Ts);
            }
            else
            {
                return oDataAcess.GetDataSet(ps_Subsidiary, ps_Loai_Ts);
            }
        }

        public DataSet GetDataSetByParent(string ps_Subsidiary, string ps_Loai_Ts, string ps_ParentCode)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return wDataAcess.GetDataSetByParent(ps_Subsidiary, ps_Loai_Ts, ps_ParentCode);
            }
            else
            {
                return oDataAcess.GetDataSetByParent(ps_Subsidiary, ps_Loai_Ts, ps_ParentCode);
            }
        }

        public DataTable GetDepreciation(string ps_Subsidiary, object pd_Ngay_Ct1, object pd_Ngay_Ct2)
        {

            DataTable table = null;
            if (AdapterInstance.IsRemoteConnected)
            {
                table = wDataAcess.GetDepreciation(ps_Subsidiary, pd_Ngay_Ct1, pd_Ngay_Ct2);
            }
            else
            {
                table = oDataAcess.GetDepreciation(ps_Subsidiary, pd_Ngay_Ct1, pd_Ngay_Ct2);
            }
            if (table != null) table.TableName = string.Empty;
            return table;
        }

        public DataTable GetDetailTable(object ps_KeyCode)
        {
            DataTable table = null;
            if (AdapterInstance.IsRemoteConnected)
            {
                table = wDataAcess.GetDetailTable(ps_KeyCode);
            }
            else
            {
                table = oDataAcess.GetDetailTable(ps_KeyCode);
            }
            if (table != null) table.TableName = string.Empty;
            return table;
        }

        public DataTable GetDetailTable0(object ps_KeyCode)
        {
            DataTable table = null;
            if (AdapterInstance.IsRemoteConnected)
            {
                table = wDataAcess.GetDetailTable0(ps_KeyCode);
            }
            else
            {
                table = oDataAcess.GetDetailTable0(ps_KeyCode);
            }
            if (table != null) table.TableName = string.Empty;
            return table;
        }

        public DataTable GetDetailTable2(object ps_KeyCode)
        {
            DataTable table = null;
            if (AdapterInstance.IsRemoteConnected)
            {
                table = wDataAcess.GetDetailTable2(ps_KeyCode);
            }
            else
            {
                table = oDataAcess.GetDetailTable2(ps_KeyCode);
            }
            if (table != null) table.TableName = string.Empty;
            return table;
        }

        public DataTable GetDetailTable3(object ps_KeyCode)
        {
            DataTable table = null;
            if (AdapterInstance.IsRemoteConnected)
            {
                table = wDataAcess.GetDetailTable3(ps_KeyCode);
            }
            else
            {
                table = oDataAcess.GetDetailTable3(ps_KeyCode);
            }
            if (table != null) table.TableName = string.Empty;
            return table;
        }

        public DataRow GetMasterDataRow(object ps_KeyCode)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return SmartSoft.DAL.SSDataAccess.ConvertTableToRow(wDataAcess.GetMasterDataRow(ps_KeyCode));
            }
            else
            {
                return oDataAcess.GetMasterDataRow(ps_KeyCode);
            }
        }

        public bool Save(bool pb_NewChecked, DataRow po_MasterDataRow, DataTable po_DetailDataTable)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                po_DetailDataTable.TableName = "VBSP";
                return wDataAcess.Save(pb_NewChecked, SmartSoft.DAL.SSDataAccess.ConvertRowToTable(po_MasterDataRow), po_DetailDataTable);
            }
            else
            {
                return oDataAcess.Save(pb_NewChecked, po_MasterDataRow, po_DetailDataTable);
            }
        }

        public bool Save3(object ps_KeyCode, DataTable po_DetailDataTable)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                po_DetailDataTable.TableName = "VBSP";
                return wDataAcess.Save3(ps_KeyCode, po_DetailDataTable);
            }
            else
            {
                return oDataAcess.Save3(ps_KeyCode, po_DetailDataTable);
            }
        }

        public bool Save4(string ps_Subsidiary, object pd_Ngay_Ct1, object pd_Ngay_Ct2, DataTable po_DetailDataTable)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                po_DetailDataTable.TableName = "VBSP";
                return wDataAcess.Save4(ps_Subsidiary, pd_Ngay_Ct1, pd_Ngay_Ct2, po_DetailDataTable);
            }
            else
            {
                return oDataAcess.Save4(ps_Subsidiary, pd_Ngay_Ct1, pd_Ngay_Ct2, po_DetailDataTable);
            }
        }


        public int SaveDetailRow0(object po_MasterKey, DataRow row)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return wDataAcess.SaveDetailRow0(po_MasterKey, SmartSoft.DAL.SSDataAccess.ConvertRowToTable(row));
            }
            else
            {
                return oDataAcess.SaveDetailRow0(po_MasterKey, row);
            }
        }

        public int SaveDetailRow2(object po_MasterKey, DataRow row)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return wDataAcess.SaveDetailRow2(po_MasterKey, SmartSoft.DAL.SSDataAccess.ConvertRowToTable(row));
            }
            else
            {
                return oDataAcess.SaveDetailRow2(po_MasterKey, row);
            }
        }

        //public int SaveMasterRow(SqlConnection po_Connection, SqlTransaction po_Transaction, bool pb_NewChecked, DataRow row)
        //{
        //    if (AdapterInstance.IsRemoteConnected)
        //    {
        //        return wDataAcess.SaveMasterRow(po_Connection, po_Transaction, pb_NewChecked, SmartSoft.DAL.SSDataAccess.ConvertRowToTable(row));
        //    }
        //    else
        //    {
        //        return oDataAcess.SaveMasterRow(po_Connection, po_Transaction, pb_NewChecked, row);
        //    }
        //}

        public int TaiSanG_Delete(object pi_MasterKey)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return wDataAcess.TaiSanG_Delete(pi_MasterKey);
            }
            else
            {
                return oDataAcess.TaiSanG_Delete(pi_MasterKey);
            }
        }

        public void TaiSanG_Get(object pi_MasterKey, ref object pd_Ngay_Giam, ref object ps_So_Ct_G, ref object ps_Ma_Giam_Ts, ref object ps_Ten_Giam_Ts)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                wDataAcess.TaiSanG_Get(pi_MasterKey, ref pd_Ngay_Giam, ref ps_So_Ct_G, ref ps_Ma_Giam_Ts, ref ps_Ten_Giam_Ts);
            }
            else
            {
                oDataAcess.TaiSanG_Get(pi_MasterKey, ref pd_Ngay_Giam, ref ps_So_Ct_G, ref ps_Ma_Giam_Ts, ref ps_Ten_Giam_Ts);
            }
        }

        public int TaiSanG_Save(object pi_MasterKey, object pd_Ngay_Giam, object ps_So_Ct_G, object ps_Ma_Giam_Ts)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return wDataAcess.TaiSanG_Save(pi_MasterKey, pd_Ngay_Giam, ps_So_Ct_G, ps_Ma_Giam_Ts);
            }
            else
            {
                return oDataAcess.TaiSanG_Save(pi_MasterKey, pd_Ngay_Giam, ps_So_Ct_G, ps_Ma_Giam_Ts);
            }
        }
    }
}